package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.config.OssConfig;
import com.honglv.context.BaseContext;
import com.honglv.mapper.FoodMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadFoodDTO;
import com.honglv.pojo.dto.UploadFoodDTO;
import com.honglv.pojo.dto.UploadFoodDTO;
import com.honglv.pojo.entity.Food;
import com.honglv.pojo.entity.Food;
import com.honglv.pojo.vo.GetFoodsByCityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.FoodService;
import com.honglv.service.FoodService;
import com.honglv.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 管理员上传美食
     * @param
     * @return
     */
    @Override
    public void upload(UploadFoodDTO uploadFoodDTO) {
        Long authorId = BaseContext.getCurrentId();
        Food food = new Food();
        BeanUtils.copyProperties(uploadFoodDTO, food);
        food.setAuthorId(authorId);
        foodMapper.insert(food);
    }
    /**
     * 根据城市分页获取美食
     * @param pageByCityDTO
     * @return
     */

    @Override
    public PageVO<GetFoodsByCityVO> getFoodsByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Food> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Food> list = foodMapper.selectList(qw);
        GetFoodsByCityVO gf=new GetFoodsByCityVO();
        List<GetFoodsByCityVO> foods=new ArrayList<>();
        for(Food food:list){
            BeanUtils.copyProperties(food,gf);
            foods.add(gf);
        }
        if (foods == null){
            return null;
        }
        PageInfo<GetFoodsByCityVO> pageInfo = new PageInfo<>(foods);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }


    }


  